<template>
  <PageDialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <div class="dialog-table">
      <PageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.searchBtn"
      >
        <template v-slot:filter-button>
          <div class="dialog-table-header">
            <div class="header-left">
              <div class="lable">产品名称：{{ info.commonName }}</div>
              <div class="lable">生产批号：{{ info.batchNumber }}</div>
            </div>
            <div class="header-right">
              <div class="scan-groups">
                <el-input
                  v-model="filterInfo.keyword"
                  :clearable="true"
                  placeholder="请输入个体码"
                  @keyup.enter.native="search"
                  @input="(e) => (filterInfo.keyword = validSe(e))"
                >
                </el-input>
                <el-button style="margin-left: 10px" @click="search"
                  >搜索</el-button
                >
              </div>
            </div>
          </div>
        </template>
      </PageFilter>
    </div>

    <dialog-table
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :check-box="false"
      :pager="false"
      :tab-index="true"
      :handle="tableInfo.handle"
      :listen-height="false"
      v-model:query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-storeBatchNumber="scope">
        <StoreBatchNumberTag :row="scope.row" />
      </template>
    </dialog-table>
  </PageDialog>
</template>

<script setup name="IndividualDetails">
import { getCustomizeSetting } from '@/api/customize'
import { purchReturnDetailsCustomizeListAll } from '@/api/purch'
import StoreBatchNumberTag from '@/views/stock/components/StoreBatchNumberTag'
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  showDialog: {
    type: Boolean,
    default: false,
  },
  api: {
    type: Function,
    default: purchReturnDetailsCustomizeListAll,
  },
  query: {
    type: Object,
    default: () => {
      return {}
    },
  },
  info: {
    type: Object,
    default: () => {
      return {}
    },
  },
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    sortString: 'product_id asc',
    queryMap: {
      ...props.query,
    },
    queryParams: {},
    customizeUserVO: {},
    keyword: '',
  },
  searchBtn: [{ slot: true, value: 'button' }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  copyData: [],
  selections: [],
  fieldList: [],
})
// 弹窗相关
const dialogInfo = ref({
  title: '个体详情',
  width: '90%',
  btLoading: false,
  btList: [{ label: '关闭', event: handleClose, show: true }],
})
const shows = computed({
  get() {
    return props.showDialog
  },
  set(val) {
    emits('update:showDialog', val) // 更新父组件shows
  },
})

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  const search = filterInfo.value.keyword
  if (search) {
    tableInfo.value.data = tableInfo.value.copyData.filter((data) => {
      return Object.keys(data).some((key) => {
        if (key === 'qrCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'uniqueCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'factorySubCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'factoryMainCode') {
          return String(data[key]).indexOf(search) > -1
        }
      })
    })
  } else {
    tableInfo.value.data = tableInfo.value.copyData
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'list':
      tableInfo.value.copyData = data
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  proxy.$emit('reset')
}
function getCustomize() {
  let param = {
    tableKey:
      'purchPurchReturnIndividualDetails-com.slip.spd.entity.vo.purch.PurchReturnDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      tableInfo.value.api = props.api
      getList()
    }
  })
}

let obj = {
  orderNo: {
    label: '单号',
    value: 'orderNo',
    componentType: 'input',
    show: false,
    // type: 'slot',
    hidden: true,
  },
  price: {
    label: '单价',
    value: 'price',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('purchReturn:price'),
    show: false,
    hidden: true,
  },
  sumAmount: {
    label: '总金额',
    value: 'sumAmount',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('purchReturn:price'),
    show: false,
    hidden: true,
  },
  defaultSupplierPrice: {
    label: '默认供应商价格',
    value: 'defaultSupplierPrice',
    type: 'column',
    permission: !proxy.$permission('purchReturn:price'),
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
  },
  storeBatchNumber: {
    label: '入库批次号',
    value: 'storeBatchNumber',
    componentType: 'input',
    type: 'slot',
    show: false,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()
</script>

<style scoped lang="scss">
:deep(.filter-item) {
  width: 100%;
}
.text-style {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.scan-groups {
  display: flex !important;
  align-items: center;
}
</style>
